//
//  LYCommonProducView.m
//  SZBank
//
//  Created by LuoYao on 16/7/25.
//  Copyright © 2016年 LuoYao. All rights reserved.
//

#import "LYCommonProducView.h"
#import "ProductTableView.h"

@interface LYCommonProducView ()<ButtonAndTableViewDelegate,UIWebViewDelegate>
{
    CGFloat ckwitdh,ckheight;
    NSMutableArray *dataArray;/**< 组装后的数据 */
    NSMutableDictionary *dataDic;/**< 组装后的数据 */
    
    NSMutableArray *values,*keys,*detailDataArray;
    NSString *lastSelectString;/**< 上一次选中的字符串 */
}
@property (nonatomic) UIImageView *backImageView;/**< 背景视图 */
@property (nonatomic) UIScrollView *topImageScrollView;/**< 顶部滑动视图 */
@property (nonatomic) UITableView *informationTableView;/**< 表格视图 */

@property (nonatomic) ButtonAndTableView *leftBtnTable;/**< 左边下拉菜单 */
@property (nonatomic) ButtonAndTableView *rightBtnTable;/**< 右边下拉菜单 */

@property (nonatomic) UIWebView *webView;/**< 网银演示 */

@end
@implementation LYCommonProducView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        ckwitdh = frame.size.width;
        ckheight = frame.size.height;
        
        
        values = [NSMutableArray array];
        keys = [NSMutableArray array];
        
        //uiimageView
        UIImageView *back = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, ckwitdh, ckheight)];
        back.image = [LocalDataManager getImageWithSourceOfPath:@"中背景"];
        back.userInteractionEnabled = YES;
        [self addSubview:back];
        
        
        [back addSubview:self.backImageView];
        
        //uiimage
        
        
        for (int i = 0; i < 3; i ++) {
            UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(ckwitdh * 0.02 + ckwitdh * 0.4 * i, 0, ckwitdh * 0.39, ckheight * 0.29)];
            imageView.image = [LocalDataManager getImageWithSourceOfPath:[NSString stringWithFormat:@"对公产品_%d",i]];
            [self.topImageScrollView addSubview:imageView];
        }
        
        [self.backImageView addSubview:self.topImageScrollView];
        
        
        
        
        dataDic = [NSMutableDictionary dictionary];
        
        //数据请求
        [NetWorkingDataManager getProductsListWithSuccess:^(id sender) {
            //数据处理
            NSArray *titleArray = @[@"企业网银", @"公司账户服务类", @"代理收付类",@"公司理财类",@"国际业务",@"公司融资类-传统类融资",@"公司结算类",@"公司存款类"];
            
            NSArray *arr = sender[@"datalist"];
            
            //遍历原始数据
            for (int i = 0; i <titleArray.count; i ++) {
                dataArray = [NSMutableArray array];
                for (int j = 0; j < arr.count; j ++) {
                    
                    if ([arr[j][@"cplx"] isEqualToString:titleArray[i]]) {
                        NSDictionary *dic = @{arr[j][@"cpmc"]:arr[j][@"id"]};
                        [dataArray addObject:dic];
                    }
                }
                [dataDic setObject:dataArray forKey:titleArray[i]];
                
            }
 
            [self.backImageView addSubview:self.leftBtnTable];
            [self.backImageView addSubview:self.rightBtnTable];
            
            
            //默认显示网银页面
            [self showWebView];
            
        } failure:nil];
        
    }
    return self;
}


- (void)showWebView{
    
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://117.172.47.171:1113/szbank/eweb/login.html"]];
    [self.webView loadRequest:request];
    [self.backImageView addSubview:self.webView];
}


#pragma mark - 下拉菜单代理协议
- (void)btnTableView:(ButtonAndTableView *)btnTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath selectTitle:(NSString *)selectTitle{
    [self.informationTableView removeFromSuperview];
    if ([[[self subviews] lastObject] isKindOfClass:[UITableView class]]){
        [[[self subviews] lastObject] removeFromSuperview];
    }
    //参数拼接
    if ([btnTableView isEqual:_leftBtnTable]) {
       //左边
        //移除原来的
        for (UIView *ve in self.backImageView.subviews) {
            if ([ve isKindOfClass:[ButtonAndTableView class]] && [ve isEqual:_rightBtnTable]) {
                [ve removeFromSuperview];
                
                [values removeAllObjects];
                [keys removeAllObjects];
            }
            
            if ([ve isKindOfClass:[UIWebView class]]) {
                [ve removeFromSuperview];
            }
        }
        
        //拼接下拉菜单需要的参数
        for (int i = 0; i < ((NSArray *)dataDic[selectTitle]).count; i ++) {
            [keys addObject:[dataDic[selectTitle][i] allKeys][0]];
            [values addObject:[dataDic[selectTitle][i] allValues][0]];
        }
        
        NSDictionary *dic = @{@"title":keys.firstObject,@"list":keys};
        
        self.rightBtnTable = [[ButtonAndTableView alloc] initWithFrame:CGRectMake(ckwitdh * 0.4, ckheight * 0.3, ckwitdh * 0.55, ckheight * 0.05) dataSource:dic backImageName:@"对公产品_11" selectImageName:@"对公产品_11"];
        self.rightBtnTable.delegate = self;
        
        [self.backImageView addSubview:self.rightBtnTable];
        
        
        //显示webView
        if ([selectTitle isEqualToString:@"企业网银"]) {
            [self showWebView];
        }
        
        
        //默认显示第一行的数据表格
        [self dataMakeWithIndex:0 selectTitle:[dataDic[selectTitle][0] allKeys][0]];
        
    }
    
    else{
        
        //异常处理，用于未选左边直接点击右边
        if (values.count == 0) {
            values = [NSMutableArray arrayWithArray:[dataDic[@"企业网银"][0] allValues]];
        }
        
        [self dataMakeWithIndex:indexPath.row selectTitle:selectTitle] ;
        
        lastSelectString = selectTitle;
    }
    
    
}


#pragma mark - 数据
- (void)dataMakeWithIndex:(NSInteger)index selectTitle:(NSString *)selectTitle{
    //组装显示数据
    NSArray *detailistTitle = @[@"产品名称",@"产品介绍",@"产品特点",@"办理条件",@"基本要素",@"适合对象",@"提交资料",@"办理流程"];
    NSArray *netTitle = @[@"cpmc",@"cpjs",@"cptd",@"bltj",@"jbys",@"shdx",@"tjzl",@"bllc"];
    detailDataArray = [NSMutableArray array];
    //右边,可以进行数据请求
    NSDictionary *dic = @{@"id":  values[index]};
    
    [NetWorkingDataManager getProductParticularsWithParam:dic success:^(id sender) {
        
        //匹配数据
        for (int i = 0; i < [sender allKeys].count; i ++) {
            
            for (int j = 0; j < netTitle.count; j ++) {
                
                NSString *title = detailistTitle[j];
                NSString *detail = sender[netTitle[j]];
                
                if ([[sender allKeys][i] isEqualToString:netTitle[j]]) {
                    
                    if (detail.length != 0) {
                        NSDictionary *dict = @{@"title":title,@"detail":detail};
                        [detailDataArray addObject:dict];
                    }
                    
                }
            }
        }
        
        NSMutableArray *muarray = [NSMutableArray array];
        
        //整理数据，按照一定顺序
        for (int i = 0; i < detailistTitle.count; i ++) {
            
            for (int j = 0; j < detailDataArray.count; j ++) {
                
                if ([detailistTitle[i] isEqualToString:detailDataArray[j][@"title"]] && ((NSString *)detailDataArray[j][@"title"]).length != 0) {
                    
                    [muarray addObject:detailDataArray[j]];
                }
            }
            
        }
        
        //先移除原有的视图
        for (UIView *view in self.backImageView.subviews) {
            if ([view isKindOfClass:[ProductTableView class]]) {
                [view removeFromSuperview];
            }
        }
        
        //显示webView
        if ([selectTitle isEqualToString:@"企业网银演示"]) {
            [self showWebView];
        }else{
            
            //展示表格
            ProductTableView *productView = [[ProductTableView alloc] initWithFrame:CGRectMake(ckwitdh * 0.05, ckheight * 0.37, ckwitdh * 0.9, ckheight * 0.63) withArray:muarray];
            
            [self.backImageView addSubview:productView];
        }
        
        
        
    } failure:nil];
}

#pragma mark - webView协议
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    return YES;
}

#pragma mark - getter
- (UIImageView *)backImageView{
    if (!_backImageView) {
        _backImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, ckwitdh, ckheight)];
        _backImageView.image = [LocalDataManager getImageWithSourceOfPath:@"对公产品背景"];
        _backImageView.userInteractionEnabled = YES;
    }
    return _backImageView;
}


- (UIScrollView *)topImageScrollView{
    if (!_topImageScrollView) {
        _topImageScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(ckwitdh * 0.02, 0, ckwitdh * 0.96, ckheight * 0.3)];
        
        _topImageScrollView.contentSize = CGSizeMake(ckwitdh * 1.2, ckheight * 0.3);
    }
    return _topImageScrollView;
}

- (ButtonAndTableView *)leftBtnTable{
    if (!_leftBtnTable) {
        _leftBtnTable = [[ButtonAndTableView alloc] initWithFrame:CGRectMake(ckwitdh * 0.05, ckheight * 0.3, ckwitdh * 0.3, ckheight * 0.05) dataSource:@{@"title":@"企业网银",@"list":[dataDic allKeys]} backImageName:@"对公产品_11" selectImageName:@"对公产品_11"];
        _leftBtnTable.delegate = self;
    }
    return _leftBtnTable;
}

- (ButtonAndTableView *)rightBtnTable{
    if (!_rightBtnTable) {
        _rightBtnTable = [[ButtonAndTableView alloc] initWithFrame:CGRectMake(ckwitdh * 0.4, ckheight * 0.3, ckwitdh * 0.55, ckheight * 0.05) dataSource:@{@"title":[dataDic[@"企业网银"][0] allKeys].firstObject,@"list":[dataDic[@"企业网银"][0] allKeys]} backImageName:@"对公产品_11" selectImageName:@"对公产品_11"];
        _rightBtnTable.delegate = self;
    }
    return _rightBtnTable;
}

- (UIWebView *)webView{
    if (!_webView) {
        _webView = [[UIWebView alloc] initWithFrame:CGRectMake(ckwitdh * 0.05, ckheight * 0.37, ckwitdh * 0.9, ckheight * 0.63)];
        _webView.delegate = self;
        _webView.scalesPageToFit = YES;
    }
    return _webView;
}

@end
